<?php

class Statistics {
	
	private $recommendationList;
	
	public function __construct(){	
		$recommendationList = array();
		$this->fillArray();
	}
	
	private function fillArray(){
		if ($handle = opendir(REP_MOD_RECOMMANDATION_FILE)) {
			$recommendationFiles = array();
	    	while (false !== ($file = readdir($handle))) {
        		if ($file != "." && $file != ".." && preg_match('/^Recommendation_.*\.php$/',$file)) {
            		$recommendationFiles[]="$file";
        		}
    		}
    		closedir($handle);
				
			if (sizeof($recommendationFiles) > 0){
				foreach($recommendationFiles as $recommendation){
					$recommendationtype = preg_replace('/\.php/','',$recommendation);
					$this->recommendationList[] = new $recommendationtype();
				}
			} else {
				throw new Exception("Erreur type Recommendation");
			}
		} else {
			throw new Exception("Erreur ouverture dossier modèle/Recommendation");
		}
		
	}
	
	public function getStats(){
		$html = "<table class=\"expendtable\"><caption>Répartition des évaluations selon les méthodes de recommandation (en %)</caption><tr><th>Méthode</th><th>aime</th><th>n'aime pas</th><th>neutre</th></tr>";
		foreach($this->recommendationList as $recommendation){
			$methode = $GLOBALS['nomsMethodeRecommendation'][$recommendation->getMethodRecommendation()];
			$total = Rating_Bd::count("score >=0 AND method_recommendation=".$recommendation->getMethodRecommendation());	
			$pAime = round(Rating_Bd::count("score>5 AND method_recommendation=".$recommendation->getMethodRecommendation())*100/$total);
			$pAimePas = round(Rating_Bd::count("score>=0 AND score<5 AND method_recommendation=".$recommendation->getMethodRecommendation())*100/$total);
			$pNeutre = round(Rating_Bd::count("score=5 AND method_recommendation=".$recommendation->getMethodRecommendation() )*100/$total);
			$html.="<tr><td class=\"tdmethod\">{$methode}</td><td>{$pAime}</td><td>{$pAimePas}</td><td>{$pNeutre}</td></tr>";
		}
		
		// Ajout de la recommandation choix utilisateur
		$methode = $GLOBALS['nomsMethodeRecommendation'][RECOMMANDATION_CHOIX_UTILISATEUR];
		$total = Rating_Bd::count("score >=0 AND method_recommendation=".RECOMMANDATION_CHOIX_UTILISATEUR);	
		$pAime = round(Rating_Bd::count("score>5 AND method_recommendation=".RECOMMANDATION_CHOIX_UTILISATEUR)*100/$total);
		$pAimePas = round(Rating_Bd::count("score>=0 AND score<5 AND method_recommendation=".RECOMMANDATION_CHOIX_UTILISATEUR)*100/$total);
		$pNeutre = round(Rating_Bd::count("score=5 AND method_recommendation=".RECOMMANDATION_CHOIX_UTILISATEUR )*100/$total);
		$html.="<tr><td class=\"tdmethod\">{$methode}</td><td>{$pAime}</td><td>{$pAimePas}</td><td>{$pNeutre}</td></tr>";
		
		
		
   		$html.="</tr></table>";

		return $html;
	}
	
}



?>